package utils;

import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.io.*;
import java.text.DecimalFormat;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;

public class Utils {
    private Utils() {

    }

    private static class SingleInner {
        private static final Utils UTILS = new Utils();
    }

    public static Utils newInstanceUtils() {
        return SingleInner.UTILS;
    }

    /**
     * 反射获取对象实力
     */
    public Object newInstanceObject(Class<?> clazz) {
        try {
            return clazz.getDeclaredConstructor().newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 从一个list中随机取出 指定个数的元素
     * @param list list
     * @param number 取出个数
     * @return 返回随机获取的list
     */
    public List<Object> randomList(List<Object> list, int number) {
        List<Object> retList = new ArrayList<>();
        if (list.isEmpty()) {
            throw new RuntimeException("list is empty!!!");
        }

        for (int i = 0; i < number; i++) {
            int size = list.size();
            int index = new Random().nextInt(size);
            retList.add(list.get(index));
            list.remove(index);
        }
        return retList;
    }

    /**
     * 将double 转换为保留2位小数的字符串
     */
    public String doubleFormat(double num) {
        DecimalFormat df = new DecimalFormat("0.00");
        return df.format(num);
    }

    /**
     * 将double 转换为保留2位小数的字符串 返回double
     */
    public double doubleFormatToDb(double num) {
        DecimalFormat df = new DecimalFormat("0.00");
        return Double.parseDouble(df.format(num));
    }
    /**
     * 将double 转换为保留2位小数的字符串 返回int
     */
    public int doubleFormatToInt(double num) {
        DecimalFormat df = new DecimalFormat("0.00");
        return Integer.parseInt(df.format(num));
    }
    /**
     * 检查文件是否存在，不存在就创建，否则删除再创建
     */
    public boolean checkFile(File file) throws IOException {
        return file.exists() ? (file.delete() && file.createNewFile()) : file.createNewFile();
    }

    /**
     * 向指定文件中输入字符串
     * @param file 文件路劲
     * @param something 字符串
     * @throws IOException
     */
    public void fileOutPut(File file, String something) throws IOException {
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file, true));
        bufferedWriter.write(something);
        bufferedWriter.newLine();
        bufferedWriter.flush();
        bufferedWriter.close();
    }

    /**
     * 读取文件内容，每行内容加入list
     * @param file 文件路径
     * @return 每行内容的list
     * @throws IOException
     */
    public List<Object> fileRead(File file) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
        String line;
        List<Object> list = new ArrayList<>();
        while ((line = bufferedReader.readLine()) != null) {
            list.add(line);
        }
        bufferedReader.close();
        return list;
    }

    public String getResource(String fileName) {
        return Objects.requireNonNull(Utils.class.getClassLoader().getResource(fileName)).getPath();
    }

    /**
     * 返回 指定位数的随机数字符串
     *
     * @param size 位数
     * @return 字符串
     */
    public String randomNum(int size) {
        StringBuilder sb = new StringBuilder();
        new Random().ints(size, 0, 10).forEach(sb::append);
        return sb.toString();
    }

    /**
     * 随机返回数组中的一个对象
     *
     * @param objects 对象数组
     * @return object
     */
    public Object randomOBJ(Object... objects) {
        return objects[new Random().nextInt(objects.length)];
    }

    /**
     * 如果2个集合中的值的差集为空 返回true（2个集合值相同）
     */
    public boolean compareCollections(Collection<Object> first, Collection<Object> sec) {
        List<String> list1 = first.stream().map(Object::toString).collect(Collectors.toList());
        List<String> list2 = sec.stream().map(Object::toString).collect(Collectors.toList());
        if (list1.size() > list2.size()) {
            list1.removeAll(list2);
            return list1.isEmpty();
        } else {
            list2.removeAll(list1);
            return list2.isEmpty();
        }
    }

    /**
     * 获取系统剪贴板内容
     */
    public String getSysClipboardText() {
        String ret = "";
        Clipboard sysClip = Toolkit.getDefaultToolkit().getSystemClipboard();
        // 获取剪切板中的内容
        Transferable clipTf = sysClip.getContents(null);

        if (clipTf != null) {
            // 检查内容是否是文本类型
            if (clipTf.isDataFlavorSupported(DataFlavor.stringFlavor)) {
                try {
                    ret = (String) clipTf
                            .getTransferData(DataFlavor.stringFlavor);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        return ret;
    }

}
